package com.lostandfound.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lostandfound.dao.PlateDao;
import com.lostandfound.entity.Plate;

/**  
* 
*  
* @date 2018年3月11日
*/
@Repository("plateDao")
public class PlateDaoImpl implements PlateDao {
	
	private Log logger = LogFactory.getLog(this.getClass());
	
	@Autowired
    private SessionFactory sessionFactory;

	@Override
	public Serializable save(Plate plate) {
		logger.info("添加了一个板块");
		return sessionFactory.getCurrentSession().save(plate);
	}

	@Override
	public void update(Plate plate) {
		logger.info("修改了一个板块");
		sessionFactory.getCurrentSession().update(plate);
	}

	@Override
	public void delete(Plate plate) {
		logger.info("删除了一个板块");
		sessionFactory.getCurrentSession().delete(plate);
	}

	@Override
	public Plate selectByPrimary(Long id) {
		return (Plate)sessionFactory.getCurrentSession().load(Plate.class, id);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List listPlateByPage(Map<String, Object> pagemap) {

		Session session = sessionFactory.getCurrentSession();
		String hql = "from Plate p where p.pstatus = '0' ";
		if(pagemap != null){
			if(pagemap.get("userCode") != null && !((String)pagemap.get("userCode")).equals("")){
				hql = hql + "and p.user.userCode like '"
						+ (String)pagemap.get("userCode") +"%'";
			}
			if(pagemap.get("pdescription") != null && !((String)pagemap.get("pdescription")).equals("")){
				hql = hql + "and p.pdescription like '"
						+ (String)pagemap.get("pdescription") +"%' ";
			}
			if(pagemap.get("pname") != null && !((String)pagemap.get("pname")).equals("")){
				hql = hql + "and p.pname like '"
						+ (String)pagemap.get("pname") +"%' ";
			}
			if(pagemap.get("mindate") != null && 
					!pagemap.get("mindate").equals("") && 
					pagemap.get("maxdate") != null && 
					!pagemap.get("maxdate").equals("")){
				hql = hql + "and p.p_create_time BETWEEN '"
						+ pagemap.get("mindate") +"' AND '"
						+ pagemap.get("maxdate") + "'";
			}
		}
		//int page1 = (int)pagemap.get("page");
		//int size1 = (int)pagemap.get("size");
		int page = Integer.parseInt((String)pagemap.get("page"));
		int size = Integer.parseInt((String)pagemap.get("size"));
		Query query = session.createQuery(hql)
				.setFirstResult((page-1)*size)
				.setMaxResults(size);
		List<?> list = query.list();

		return list;
	}

	@Override
	public int Count(Map<String, Object> pagemap) {
		String hql = "select count(*) from Plate p where p.pstatus = '0' ";
		if(pagemap != null){
			if(pagemap.get("userCode") != null  && !((String)pagemap.get("userCode")).equals("")){
				hql = hql + "and p.user.userCode like '"
						+ (String)pagemap.get("userCode") +"%'";
			}
			if(pagemap.get("pdescription") != null  && !((String)pagemap.get("pdescription")).equals("")){
				hql = hql + "and p.pdescription like '"
						+ (String)pagemap.get("pdescription") +"%' ";
			}
			if(pagemap.get("mindate") != null && 
					!pagemap.get("mindate").equals("") && 
					pagemap.get("maxdate") != null && 
					!pagemap.get("maxdate").equals("")){
				hql = hql + "and p.p_create_time BETWEEN '"
						+ pagemap.get("mindate") +"' AND '"
						+ pagemap.get("maxdate") + "'";
			}
		}
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		return ((Number) query.iterate().next())
	  	         .intValue();
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List getPlateByName(String pname) {
		Session session = sessionFactory.getCurrentSession();
		String hql = "select p from Plate p where p.pstatus = '0' and p.pname = ?";
		Query query = session.createQuery(hql);
		query.setString(0, pname);
		
		return query.list();
	}

	/**
	 * 执行批量删除操作
	 */
	@Override
	public void deletePlateByBranch(Long[] plates) {
		String hql = "";
		for(int i=0;i<plates.length;i++) {
            if(i==0) {
                hql = "id="+plates[i];
            } else {
                hql =hql + " or id="+plates[i];
            }
        }
		if(!hql.equals("")){
	        Session session= sessionFactory.getCurrentSession();
	        Query q= session.createQuery("update Plate p set p.pstatus = '1' where "+hql);
	        q.executeUpdate();
			logger.info("批量删除板块" + plates);
		}
	}

	@Override
	public List<?> getAllPlate() {
		return sessionFactory.getCurrentSession()
				.createQuery("select p from Plate p where p.pstatus = '0' ")
				.list();
	}

}
